<?php

namespace app\http\controller;
use think\swoole\Server;
use app\http\controller\Carapi;
use think\Controller;
use think\Db;
use think\facade\Log;
ini_set('default_socket_timeout', -1);
class Package extends Controller{
    public function issued(){
        $redis = new \Redis();
        $redis->pconnect('127.0.0.1',6379);
        $redis->auth('zaozao2019@');
        $redis->subscribe(array('car_package'), array($this,"callback"));
        
       
    }
    
     // 回调函数,这里写处理逻辑
    function callback($instance, $channelName, $message) {
        $arrid=json_decode($message);

        foreach($arrid as $key=>$value){
            
            $carinfo=Db::table("car_package")->where("id",$value)->find();
            Log::write($carinfo,"carinfo",true);
            if(!empty($carinfo))
            {
                
                $arr["bizCode"]="B1031";
                $arr["parkingNo"]=$carinfo["parkingNo"];
                $arr["reqNo"]=date("YmdHis")."-".rand(100000,999999);
                $arr["card_id"]=$carinfo["id"];
                $arr["create_time"]=strtotime($carinfo["create_at"]);
                $arr["update_time"]=strtotime($carinfo["create_at"]);
                $arr["operate_type"]=$carinfo["operate_type"];
                if($arr["operate_type"]=="2"){
                    $arr["update_time"]=strtotime($carinfo["update_at"]);;
                }

                
                $arr["begin_time"]=$carinfo["begin_time"];
                $arr["end_time"]=$carinfo["end_time"];
                $arr["car_number"]=$carinfo["car_number"];
                $arr["name"]=$carinfo["owner_name"];
                $arr["tel"]=$carinfo["owner_phone"];
                $arr["price"]=$carinfo["price"];
                $arr["remark"]=trim($carinfo["remarks"]);
                $arr["address"]=$carinfo["address"];

                $arr["limit_day_type"]="0";
                
                //查询改车场所有的客户端
                
                $clientList=Db::table("parking_client")->where("parkingNo",$carinfo["parkingNo"])->field("clientNo")->select();
                
                $clientNo="";
                foreach ($clientList as $clientinfo){
                    $clientNo=$clientNo.$clientinfo["clientNo"].",";
                }
                
                $clientNo=substr($clientNo,0,strlen($clientNo)-1);
                
                $arr["clientNo"]=$clientNo;
                $arr["amount_receivable"]="0";
                $parkingKey=$carinfo["parkingNo"];
                $arr["sign"]=$this->MakeSign($arr,$parkingKey);
                
                $result=$this->jsondata($arr);
                echo $result."\n\n";
                
                $client=new \swoole_client(SWOOLE_SOCK_TCP);
                if(!$client->Connect("47.106.184.188",9500,20)){
                    echo "errCode:".$client->errCode."\r\n";
                    $path='/www/wwwroot/parkingcommunity/runtime/log/'.date('Ymd');
                    if (! file_exists ( $path )) {
                        mkdir ( "$path", 0777, true );
                    }
                    file_put_contents($path."/log.txt",date("Y-m-d H:i:s")."errcode:\r\n".$client->errCode."\r\n",FILE_APPEND);
                    die("connect failed\r\n");
                }
               
                $client->send($result."\r\n");
                //从服务端接收信息
                $recdata=$client->recv();
                if(!$recdata){
                    return "查询失败,请稍后再试";
                    die("recv failed \r\n");
                }
                $recdata=json_decode($recdata,true);
                if($recdata["bizCode"]=="B1032"){
                    if($recdata["respCode"]=="00000"){
                        if($carinfo["operate_type"]=="3"){
                            Db::table("car_package")->where("id",$carinfo["id"])->delete();
                        }else{
                            Db::table("car_package")->where("id",$carinfo["id"])->update(["delivery_status"=>"1"]);
                        }

                    }
                }
                $client->close();
            }else{
                echo 2;
            }
            
        }
    
    }
    
    
    private function jsondata($arr){
        $str='{';
        foreach($arr as $key =>$value){
            if($key=="create_time"||$key=="begin_time"||$key=="end_time"){
                $str=$str.'"'.$key.'":'.$value.',';
            }else{
                $str=$str.'"'.$key.'":"'.$value.'",';
            }
            
        }
        $str = trim($str, ",");
        $str=$str.'}';
        return $str;
    }
    private function MakeSign($arr,$parkingKey){
        //签名步骤一：按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);
        //return $string;
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $parkingKey;
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    
    //格式化参数格式化成url参数(str)
    private function ToUrlParams($value){
        $buff = "";
        foreach($value as $k => $v){
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= strtolower($k) . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
}